<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="gly"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
<meta charset="GB18030">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet"
	href="${pageContext.request.contextPath}/static/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/static/css/font-awesome.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/static/css/main.css">

</head>
<body style="margin-top: -50px">
	<div class="panel panel-default" style="margin-top: 0px">
		<div class="panel-heading">
			<h3 class="panel-title">
				<i class="glyphicon glyphicon-th"></i> 往来单位
			</h3>
		</div>
		
		<div class="panel-body">
			<!-- 查询start -->
			<form class="form-inline" role="form" style="float: left;" method="post" action="${pageContext.request.contextPath}/basics/getByCondition">
				<div class="form-group has-feedback">
					<div class="input-group">
						<div class="input-group-addon">查询依据</div>
						<select class="form-control has-success" name="un_conditionBy">
							<option value="un_name" ${sessionScope.un_conditionBy=="un_name"?"selected":""}>单位名</option>
							<option value="un_type" ${sessionScope.un_conditionBy=="un_type"?"selected":""}>单位类型</option>
						</select>
					</div>
				</div>
				<div class="form-group has-feedback">
					<div class="input-group">
						<div class="input-group-addon">查询条件</div>
						<input class="form-control has-success" type="text"
							placeholder="请输入查询条件，支持模糊查询" name="un_condition" value="${sessionScope.un_condition}">
					</div>
				</div>
				<button type="submit" class="btn btn-warning">
					<i class="glyphicon glyphicon-search"></i> 查询
				</button>
			</form>
			<!-- 查询end -->
			
<form action="" method="post">
			<button type="submit" class="btn btn-danger"
				style="float: right; margin-left: 10px;" id="sub1">
				<i class=" glyphicon glyphicon-remove"></i> 批量删除
			</button>
			<a class="btn btn-primary" style="float: right; margin-left: 10px;"
				href="" id="adduseunit" data-toggle="modal"
			data-target="#addModal"><span
				class="glyphicon glyphicon-plus"></span>增加</a> <br>
			<hr style="clear: both;">
			<div class="table-responsive">
				<table class="table  table-bordered">
					<thead>
						<tr>
							<th width="30">#</th>
							<th width="30"><input type="checkbox" id="selectAll"></th>
							<th>单位名称</th>
							<th>单位类型</th>
							<th>单位电话</th>
							<th>联系人</th>
							<th>是否启用</th>
							<th width="100">操作</th>
						</tr>
					</thead>
					<tbody id="tbody1">					
							<!-- ajax 拼接内容 -->
					<gly:forEach items="${pageInfo.list}" var="useunit" varStatus="vs">
					<tr><td>${vs.count}</td>
					<td><input type='checkbox' value="${useunit.unId}" name='cb'></td>
					<td>${useunit.unName}</td>
					<td>${useunit.unType}</td>
					<td>${useunit.unPhone}</td>
					<td>${useunit.unUser}</td>
					<td>${useunit.unIsuse}</td>
					<td><button type='button' class='btn btn-success btn-xs'><i class=' glyphicon glyphicon-list-alt'></i></button>
					<button type='button' class='btn btn-primary btn-xs' ><i class=' glyphicon glyphicon-pencil'></i></button>
					<button type='button' class='btn btn-danger btn-xs'><i class=' glyphicon glyphicon-remove'></i></button></td></tr>
					</gly:forEach>
					</tbody>

				</table>
			</div>
			</form>
			
			<!-- 增加模态框开始 -->
	<div class="modal fade" id="addModal" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">往来单位增加</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" role="form" id="add_useunit_form">
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">单位名称</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="unName"
									placeholder="请输入单位名称" name="unName">
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">单位类型</label>
							<div class="col-sm-10">
								<select class="form-control" name="unType">
							
				</select>
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">地址</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="unAddress"
									placeholder="请输入单位地址" name="unAddress">
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">电话</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="unPhone"
									placeholder="请输入联系人电话" name="unPhone">
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">联系人</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="unUser"
									placeholder="请输入联系人名称" name="unUser">
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">备注</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="unRemark"
									placeholder="请输入备注" name="unRemark">
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">状态</label>
							<div class="col-sm-10">
								<input type="radio" class="form-control"  name="unIsuse" value="是" checked="checked">启用
								<input type="radio" class="form-control"  name="unIsuse" value="否">禁用
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" id="add_useunit_btn" >保存</button>
				</div>
				
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->
	<!-- 增加结束 -->
			
			
	<!-- 修改模态框开始 -->
	<div class="modal fade" id="updModal" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">往来单位修改</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" role="form" id="upd_useunit_form">
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">单位名称</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="unName2"
									placeholder="请输入单位名称" name="unName">
									
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">单位类型</label>
							<div class="col-sm-10">
								<select class="form-control" name="unType" id="unType" >
							
								</select>
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">地址</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="unAddress2"
									placeholder="请输入单位地址" name="unAddress">
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">电话</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="unPhone2"
									placeholder="请输入联系人电话" name="unPhone">
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">联系人</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="unUser2"
									placeholder="请输入联系人名称" name="unUser">
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">备注</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="unRemark2"
									placeholder="请输入备注" name="unRemark">
									<input type='hidden' name='unId' value=''>
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">状态</label>
							<div class="col-sm-10">
								<input type="radio" class=""  name="unIsuse1" value="是" checked="checked">启用
								<input type="radio" class=""  name="unIsuse1" value="否">禁用
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" id="upd_useunit_btn" >保存</button>
				</div>
				
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->
	<!-- 修改结束 -->
	
	<!-- 详细信息模态框开始 -->
	<div class="modal fade" id="showModal" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">往来单位修改</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" role="form" id="upd_useunit_form">
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">单位名称</label>
							<div class="col-sm-10" id="unName3" style="margin-top: 7px">
									
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">单位类型</label>
							<div class="col-sm-10" id="unType3" style="margin-top: 7px">
								
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">地址</label>
							<div class="col-sm-10" id="unAddress3" style="margin-top: 7px">
								
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">电话</label>
							<div class="col-sm-10" id="unPhone3" style="margin-top: 7px">
								
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">联系人</label>
							<div class="col-sm-10" id="unUser3" style="margin-top: 7px">
								
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">备注</label>
							<div class="col-sm-10" id="unRemark3" style="margin-top: 7px">
								
							</div>
						</div>
						<div class="form-group">
							<label for="dname" class="col-sm-2 control-label">状态</label>
							<div class="col-sm-10" id="unIsuse3" style="margin-top: 7px">
								
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					
				</div>
				
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->
	<!-- 详细信息展示结束 -->
	
			<div class="row">
				<!--文字信息-->

				<div align="center">
					<ul class="pagination">

						<li><a href="${pageContext.request.contextPath}/basics/getByCondition?un_pn=1">首页</a></li>

						<!--上一页-->
						<li><gly:if test="${pageInfo.hasPreviousPage}">
								<a
									href="${pageContext.request.contextPath}/basics/getByCondition?un_pn=${pageInfo.pageNum-1}"
									aria-label="Previous"> <span aria-hidden="true">«</span>
								</a>
							</gly:if></li>

						<!--循环遍历连续显示的页面，若是当前页就高亮显示，并且没有链接-->
						<gly:forEach items="${pageInfo.navigatepageNums}" var="page_num">
							<gly:if test="${page_num == pageInfo.pageNum}">
								<li class="active"><a href="#">${page_num}</a></li>
							</gly:if>
							<gly:if test="${page_num != pageInfo.pageNum}">
								<li><a
									href="${pageContext.request.contextPath}/basics/getByCondition?un_pn=${page_num}">${page_num}</a></li>
							</gly:if>
						</gly:forEach>

						<!--下一页-->
						<li><gly:if test="${pageInfo.hasNextPage}">
								<a
									href="${pageContext.request.contextPath}/basics/getByCondition?un_pn=${pageInfo.pageNum+1}"
									aria-label="Next"> <span aria-hidden="true">»</span>
								</a>
							</gly:if></li>

						<li><a
							href="${pageContext.request.contextPath}/basics/getByCondition?un_pn=${pageInfo.pages}">尾页</a></li>
					</ul>
				</div>
				<div align="center">当前第 ${pageInfo.pageNum} 页.总共
					${pageInfo.pages} 页.一共 ${pageInfo.total} 条记录</div>
			</div>
		</div>

	</div>
	<script
		src="${pageContext.request.contextPath}/static/jquery/jquery-2.1.1.min.js"></script>
	<script
		src="${pageContext.request.contextPath}/static/bootstrap/js/bootstrap.min.js"></script>
	<script
		src="${pageContext.request.contextPath}/static/script/docs.min.js"></script>
	<script type="text/javascript">
	$(function(){
		//UseunitList();
		upd1();
		deleteone();
		Add();
		Add2();
		upd2();
		deletemany();
		show();
		
		$("#selectAll").change(function(){
			if($(this).prop('checked')==true){
				$('input[name="cb"]').each(function(){
				$(this).prop('checked',true);
			})
			}else{
				$('input[name="cb"]').each(function(){
					$(this).prop('checked',false);
				})
			}
			
		});
		
		
		
	})
	
	function Add(){
		$("#adduseunit").click(function(){
			$.ajax({
				url:"useunitadd1",
				type:"post",
				dataType:"json",
				async:true,
				success:function(data){
					var str="";
					str+="<option>选择类型</option>";
					for(var i=0;i<data.length;i++){
						str+="<option value="+data[i]+">"+data[i]+"</option>";
					}
					$("[name='unType']").html(str);
				}
			})
		})
	}
	function Add2(){
		$("#add_useunit_btn").click(function(){
			$.ajax({
				url:"useunitadd2",
				type:"post",
				data:$("#add_useunit_form").serialize(),
				async:true,
				success:function(data){
					
					$("#addModal").modal('hide');
					window.location.href='${pageContext.request.contextPath}/basics/getByCondition?un_pn=${pageInfo.pageNum}';
				}
			})
		})
	}
	/* 修改1 */
	function upd1(){
		$("[class='btn btn-primary btn-xs']").click(function(){
			var i =$(this).parent().siblings().first().next().children().val();
			
			$.ajax({
				url:"useunitupd1",
				type:"post",
				data:{"unId":i},
				async:true,
				dataType:"json",
				success:function(data){
					var str="";
					var u =data.use;
					var l=data.type;
					
					str+="<option>"+u.unType+"</option>";
					for(var i=0;i<l.length;i++){
						str+="<option value="+l[i]+">"+l[i]+"</option>";
					}
					$("#unType").html(str);
					
					$("#unName2").val(u.unName);
					
					$("#unAddress2").val(u.unAddress);
					$("#unPhone2").val(u.unPhone);
					$("#unUser2").val(u.unUser);
					$("#unRemark2").val(u.unRemark);
					$("[name='unIsuse']").val(u.unIsuse);
					$("[name='unId']").val(u.unId);
				}
			})
			$('#updModal').modal('show');
		})
	}
	/* 修改2 */
	function upd2(){
		$("#upd_useunit_btn").click(function(){
			
			$.ajax({
				url:"useunitupd2",
				type:"post",
				data:$("#upd_useunit_form").serialize(),
				async:true,
				success:function(data){
					
					$("#updModal").modal('hide');
					window.location.href='${pageContext.request.contextPath}/basics/getByCondition?un_pn=${pageInfo.pageNum}';
				}
			})
		})
	}
	
	/* 删除一个 */
	function deleteone(){
		$("[class='btn btn-danger btn-xs']").click(function(){
			if (confirm("是否删除选中记录?")==false) {
				return ;
			}
			var i =$(this).parent().siblings().first().next().children().val();
			
			$.ajax({
				url:"useunitdeleteone",
				type:"post",
				data:{"unId":i},
				async:true,
				success:function(data){
										
					window.location.href='${pageContext.request.contextPath}/basics/getByCondition?un_pn=${pageInfo.pageNum}';
				}
			})
		})
	}
	/* 删除多个 */
	function deletemany(){
		$("#sub1").click(function(){
			
			var flag=false;
			$('input[name="cb"]').each(function(){
				if($(this).prop('checked')==true){
					flag=true;
				}
			})
			if (flag==false) {
				alert("请选择删除项目！");
			return false;
			}
			if (confirm("是否删除选中记录?")==false) {
				return;
			}
			var arr=[];
			$(":checkbox[name='cb']:checked").each(function(){
				arr.push($(this).val());
			})
			$.ajax({
				url:"useunitdeletemany",
				type:"post",
				data:{"arr":arr.join(",")},
				async:true,
				success:function(data){
					alert("成功删除"+data+"条记录");					
					window.location.href='${pageContext.request.contextPath}/basics/getByCondition?un_pn=${pageInfo.pageNum}';
				}
			})
		})
	}
	
	function show(){
		$("[class='btn btn-success btn-xs']").click(function(){
			var i =$(this).parent().siblings().first().next().children().val();
			
			$.ajax({
				url:"useunitshow",
				type:"post",
				data:{"unId":i},
				async:true,
				dataType:"json",
				success:function(u){
				
					$("#unName3").text(u.unName);
					$("#unType3").text(u.unType);
					$("#unAddress3").text(u.unAddress);
					$("#unPhone3").text(u.unPhone);
					$("#unUser3").text(u.unUser);
					$("#unRemark3").text(u.unRemark);
					$("#unIsuse3").text(u.unIsuse);
				
				}
			})
			$('#showModal').modal('show');
		})
	}
		</script>
</body>
</html>